package cn.com.zpon.shouyinyuandemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import cn.com.zpon.android.PayResult;
import cn.com.zpon.android.ShouYinYuanpp;
import cn.com.zpon.shouyinyuandemo.R;
import cn.com.zpon.shouyinyuandemo.bean.PayConstant;
import cn.com.zpon.shouyinyuandemo.net.ServiceApi;
import cn.com.zpon.shouyinyuandemo.net.callback.BaseCallback;
import cn.com.zpon.shouyinyuandemo.util.ToastUtil;

public class MainActivity extends BaseActivity {

    Button wechat_btn;
    Button alipy_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        wechat_btn = (Button) findViewById(R.id.wechat_bt);
        alipy_btn = (Button) findViewById(R.id.alipy_bt);

        wechat_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchOrderInfo(PayConstant.PAY_TYPE_WX, 1, "商品");
            }
        });

        alipy_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fetchOrderInfo(PayConstant.PAY_TYPE_ALIPAY, 1, "商品");
            }
        });

        ShouYinYuanpp.init(true, PayConstant.WXAPPID);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1010) {//支付回调结果
            if (resultCode == 1011 && data != null) {
                PayResult result = (PayResult) data.getSerializableExtra("payresult");
                String resultStatus = result.getResultStatus();
                //resultStatus 0:支付成功  －1:支付失败 -2:支付取消
                if (result != null) {
                    Log.i("pay", result.toString());
                } else {
                    Log.i("pay", "支付回调异常");
                }
            }
        }
    }

    private void fetchOrderInfo(final String channel, int amount, String subject) {
        showProgressDialog("获取订单信息中...", null);
        ServiceApi.getInstace().fetchOrderInfo(channel, amount, subject, new BaseCallback() {
            @Override
            public void requestSuccess(String order) {
                dismissProgressDialog();
                if (order == null) {
                    ToastUtil.showToast("获取订单失败");
                } else {
                    ShouYinYuanpp.createPayment(channel, MainActivity.this, order);
                }
            }

            @Override
            public void requestFail(String result) {
                dismissProgressDialog();
                ToastUtil.showToast("获取订单失败");
            }

            @Override
            public void requestfinish() {
                dismissProgressDialog();
            }
        });

    }
}
